package com.gw2.gw2world.web.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


public class Downloader {
	/** Download a page. Call it in a thread.
	 * @param url Page URL
	 * @return Downloaded data
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static String download(String url) throws ClientProtocolException, IOException {
		org.apache.log4j.Logger.getLogger("com.gw2.gw2world.web.server.cron").info("Downloading "+url);
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(url);
		HttpResponse response = client.execute(request);
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		StringBuilder sb=new StringBuilder();
		String line = "";
		while ((line = rd.readLine()) != null) {
		  sb.append(line);
		}
		return sb.toString();
	}
}
